package 多线程;

import org.junit.Test;

import java.util.HashMap;

public class 模拟死锁 {
    private final static String A = "A";
    private final static String B = "B";

    @Test
    public void test() throws InterruptedException {
        Thread threadA = new Thread(()->{
            synchronized(A){
                try {
                    Thread.sleep(100);
                    synchronized (B){
                        System.out.println("Get");
                    }
                } catch (InterruptedException e) { }
            }
        });
        Thread threadB = new Thread(()->{
            synchronized(B){
                try {
                    Thread.sleep(100);
                    synchronized (A){
                        System.out.println("Get");
                    }
                } catch (InterruptedException e) { }
            }
        });
        threadA.start();
        threadB.start();
        threadA.join();
        System.out.println("END");
        HashMap map = new HashMap();
        map.size();
    }

    @Test
    public void 测试当自己再次获取相同锁时是否会死锁(){
        Object anchor = new Object();
        synchronized (anchor){
            System.out.println("A");
            synchronized (anchor){
                System.out.println("B");
            }
        }
    }
    // 结论是：不会
}
